//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.ControlPanel.Controls;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using ComponentArt.Web.UI;
using ResourceControl = CommunityServer.ControlPanel.Controls.ResourceControl;
using ResourceLabel = CommunityServer.ControlPanel.Controls.ResourceLabel;

namespace CommunityServer.ControlPanel.Blogs
{
	/// <summary>
	/// Summary description for TokensPage.
	/// </summary>
	public class TokensPage : BaseBlogPage
	{
		protected ControlPanelSelectedNavigation SelectedNavigation1;
		protected ResourceLabel title;
		protected MPContent DescriptionRegion;
		protected MPContent TaskRegion;
		protected ResourceControl Resourcecontrol2;
		protected MPContainer MPContainer;
        protected Repeater theTokens;

		override protected void OnInit(EventArgs e)
		{
			this.Load += new EventHandler(this.Page_Load);
			base.OnInit(e);
		}

		private void Page_Load(object sender, EventArgs e)
		{
            theTokens.ItemCommand +=new RepeaterCommandEventHandler(theTokens_ItemCommand);
            theTokens.ItemCreated +=new RepeaterItemEventHandler(theTokens_ItemCreated);
            JavaScript.RegisterRefresh(this.Page);
            if(!Page.IsPostBack && !IsCallBack)
            {
                buildGrid();
                    
            }
		}

        protected void buildGrid()
        {
            ArrayList tokens = TextTokens.GetTokens(this.CurrentWeblog.SectionID);
            theTokens.DataSource = tokens;
            theTokens.DataBind();
        }

        private void theTokens_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if(e.CommandName == "Delete")
            {
                int tokenID = Globals.SafeInt(e.CommandArgument.ToString(), -1);
                if(tokenID > -1)
                {
                    ArrayList tokens = TextTokens.GetTokens(this.CurrentWeblog.SectionID);
                    foreach(TextToken token in tokens)
                    {
                        if(token.TokenID == tokenID)
                        {
                            TextTokens.Delete(token);
                            break;
                        }
                    }
                }

            }

            Response.Redirect(Request.Url.ToString());
            Response.End();
        }

        private void theTokens_ItemCreated(object sender, RepeaterItemEventArgs e)
        {
            if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                LinkButton lb = e.Item.FindControl("DeleteButton") as LinkButton;
                if(lb != null)
                {
                    lb.Attributes.Add("onclick", "return confirm('" + CommunityServer.ControlPanel.Components.ResourceManager.GetString("CP_Blog_TextParts_DeleteWarning") + "');");
                }
            }
        }
    }
}
